#region Copyright (C) 2007 Jens Winter

//Copyright (C) 2007 Jens Winter

//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.

//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#endregion

using System;
using System.Data;
using Castle.MicroKernel;
using Flynn.Core.TaskResults;
using Flynn.Core.Tasks;
using MbUnit.Framework;
using Rhino.Mocks;

namespace Flynn.Core.Tests.Tasks
{
    [TestFixture]
    public class PrepareDatabaseTaskFixture
    {
        private MockRepository _mocks;

        private IKernel _kernel;
        private PrepareDatabaseTask _prepareDatabaseTask;

        [SetUp]
        public void SetUp()
        {
            _mocks = new MockRepository();

            _kernel = new DefaultKernel();
            _prepareDatabaseTask = new PrepareDatabaseTask(_kernel);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Kernel_Must_Not_Be_Null()
        {
            new PrepareDatabaseTask(null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void When_Preparing_Database_Project_Must_Not_Be_Null()
        {
            _prepareDatabaseTask.PrepareDatabase(null, _mocks.Stub<IDbConnection>());
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void When_Preparing_Database_Connection_Must_Not_Be_Null()
        {
            _prepareDatabaseTask.PrepareDatabase(new Project(), null);
        }

        [Test]
        public void Can_Prepare_Database()
        {
            Project project = new Project { Versioning = "FakeVersioning" };

            IDbConnection connection = _mocks.Stub<IDbConnection>();

            IVersioning versioning = _mocks.CreateMock<IVersioning>();
            _kernel.AddComponentInstance("Versioning.FakeVersioning", typeof(IVersioning), versioning);

            using (_mocks.Record())
            {
                versioning.PrepareDatabase(connection);
            }

            using (_mocks.Playback())
            {
                PreparingDatabaseResult result = _prepareDatabaseTask.PrepareDatabase(project, connection);

                Assert.IsFalse(result.Failed);
            }
        }

        [Test]
        public void Preparing_Database_Fails_If_Versioning_Is_Unknown()
        {
            Project project = new Project { Versioning = "UnknownVersioning" };

            PreparingDatabaseResult result = _prepareDatabaseTask.PrepareDatabase(project, _mocks.Stub<IDbConnection>());

            Assert.IsTrue(result.Failed);
            Assert.AreEqual("Versioning 'UnknownVersioning' is unknown.", result.FailureMessage);
        }
        
        [Test]
        public void Preparing_Database_Fails_If_Exception_Is_Thrown()
        {
            Project project = new Project { Versioning = "FakeVersioning" };

            IDbConnection connection = _mocks.Stub<IDbConnection>();

            IVersioning versioning = _mocks.CreateMock<IVersioning>();
            _kernel.AddComponentInstance("Versioning.FakeVersioning", typeof(IVersioning), versioning);

            using (_mocks.Record())
            {
                versioning.PrepareDatabase(connection);
                LastCall.Throw(new Exception("Failure message"));
            }

            using (_mocks.Playback())
            {
                PreparingDatabaseResult result = _prepareDatabaseTask.PrepareDatabase(project, connection);

                Assert.IsTrue(result.Failed);
                Assert.AreEqual("Failure message", result.FailureMessage);
            }
        }
    }
}